package com.sailorj.itools.common;

import org.json.simple.JSONObject;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ExecutorGitService {

    public static void main(String[] args) {
        doExecutor();
    }

    public static void doExecutor() {
        try {
            JSONObject result = new JSONObject();
            // 开多个线程 获取数据后整合
            ExecutorService executorService = Executors.newFixedThreadPool(10);

            executorService.submit(() -> {
                result.put("1", "1");
                System.out.println(result.toJSONString());
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            executorService.submit(() -> {
                result.put("2", "2");
                System.out.println(result.toJSONString());
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });

            executorService.shutdown();
            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);

            System.out.println(result.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
